package com.yohann.boot.fatories;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.List;

/**
 * @author Yeung Wah
 * @since 2021/12/27
 */
public class FactoriesListener implements ApplicationListener<ApplicationEvent> {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationStartedEvent) {
            onApplicationStartedEvent();
        }
    }

    private void onApplicationStartedEvent() {
        List<FactoriesTest> factoriesTests = SpringFactoriesLoader.loadFactories(FactoriesTest.class, getClass().getClassLoader());
        for (FactoriesTest factoriesTest : factoriesTests) {
            factoriesTest.test();
        }
    }
}